<template>
	<view class="w-1 p-30">
		<view class="w-1 aitem m-b-20" @click="clickitem(item)" v-for="(item,index) in list" :key="index">
			<view class="fr-s-c bb-solid w-1 p-25">
				<view class="flex-1 c-00 fs-26 fl-c-s">
					<text>联系人：{{item.name}}</text>
					<text class="text-line-2">上门地址：{{item.residevice+item.residecity+item.residedist+item.address}}</text>
				</view>
				<image @click="$to('/pages/User/Address/EditAddress?info='+JSON.stringify(item))" class="m-l-20" style="width: 41rpx; height: 41rpx;" src="/static/images/tab2/edit.png" mode=""></image>
			</view>
			<view class="w-1 fr-b-c" style="padding: 20rpx;">
				<view class="fr-s-c" @click.stop="setdef(item)">
					<image v-if="item.is_defult==1" style="width: 40rpx; height: 40rpx;" src="/static/images/login/y.png" mode=""></image>
					<image v-else style="width: 40rpx; height: 40rpx;" src="/static/images/tab2/pn.png" mode=""></image>
					<text class="c-00 fs-26 bold m-l-10">设为默认</text>
				</view>
				<view class="fr-s-c" @click.stop="del(item)">
					<up-icon name="trash" color="#999999"></up-icon>
					<text class="c-99 fs-26" style="margin-left: 3rpx;">删除</text>
				</view>
			</view>
		</view>
		<view class="w-1 p-t-150" v-if="nodata">
			<up-empty :icon="$icon"></up-empty>
		</view>
		<view style="height: 200rpx;"></view>
		<view class="w-1 iphonex">
			<view class="w-1 p-30">
				<view class="big-btn" @click="$to('./AddAddress')">添加新地址</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	uni.$on('reads',()=>{
		getlist(false)
	})
	
	const choose = ref(0)
	const ekey = ref("")
	onLoad(o=>{
		choose.value = o.choose || 0;
		ekey.value = o.ekey || 0;
		getlist()
	})
	onUnload(()=>{
		uni.$off('reads')
	})
	
	function clickitem(item){
		if(ekey.value){
			uni.$emit(ekey.value,item)
			uni.navigateBack()
		}
	}
	const list = ref([])
	const nodata = ref(false)
	function getlist(loading=true){
		uni.$post({
			url:'/api/Address/dressselect',
			loading
		}).then(res=>{
			list.value = res.data;
			nodata.value = !list.value.length;
		})
	}
	
	
	function del(item){
		uni.showModal({
			title:"提示",
			content:'确定删除吗？',
			success: (res) => {
				if(res.confirm){
					uni.$post({
						url:'/api/Address/deldress',
						data:{
							did:item.id
						}
					}).then(res=>{
						getlist(false)
					})
				}
			}
		})
	}
	
	function setdef(item){
		if(item.is_defult==1) return;
		uni.$post({
			url:'/api/Address/upadress',
			data:{
				did:item.id,
				deadress:1
			}
		}).then(res=>{
			getlist(false)
		})
	}
</script>

<style>
page{
	background: #f7f7f7;
}

.aitem{
	background: #FFFFFF;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
}
</style>
